/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.common.ReflectUtils;
import com.fujitsu.nanjing.tieba.constrant.CommonParamter;
import com.fujitsu.nanjing.tieba.domain.ReplyInfo;
import com.fujitsu.nanjing.tieba.ibator.CoreReply;
import com.fujitsu.nanjing.tieba.ibator.CoreReplyCriteria;
import com.fujitsu.nanjing.tieba.ibator.CoreUser;

/**
 * ReplyInfoService.java
 * 
 * @author Qixiaobo-win8
 */
@Service
@Transactional
public class ReplyInfoService extends BaseService<ReplyInfo, CoreReplyCriteria> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(ReplyInfoService.class);

	@Resource
	private UserService userService;

	@Resource
	private ReplyService replyService;

	@Resource
	private AttachService attachService;

	@Override
	public ReplyInfo findById(String id) {
		CoreReply reply = replyService.findById(id);
		ReplyInfo replyInfo = new ReplyInfo();
		try {
			ReflectUtils.copy(reply, replyInfo);
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		CoreUser user = userService.findById(reply.getUserid());
		replyInfo.setUser(user);
		replyInfo.setAttachInfos(attachService
				.getAllAttachesByReplyId(replyInfo.getReplyid()));
		return replyInfo;
	}

	@Override
	public ReplyInfo insert(ReplyInfo t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(ReplyInfo t) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(ReplyInfo t) {
		// TODO Auto-generated method stub

	}
	/**
	 * 获取第一页的回复
	 * */
	public List<ReplyInfo> findReplyInfosByTopicId(int topicId) {
		List<CoreReply> coreReplies = replyService.findRepliesByTopicId(
				topicId, CommonParamter.起始页码);
		ReplyInfo info = null;
		List<ReplyInfo> replyInfos = new ArrayList<ReplyInfo>();
		for (CoreReply coreReply : coreReplies) {
			info = new ReplyInfo();
			info = findById(coreReply.getReplyid().toString());
			replyInfos.add(info);
		}
		return replyInfos;
	}

	public List<ReplyInfo> findReplyInfosByTopicIdDefault(int topicId,
			int offset) {
		List<CoreReply> coreReplies = replyService.findRepliesByTopicId(
				topicId, offset);
		ReplyInfo info = null;
		List<ReplyInfo> replyInfos = new ArrayList<ReplyInfo>();
		for (CoreReply coreReply : coreReplies) {
			info = new ReplyInfo();
			info = findById(coreReply.getReplyid().toString());
			replyInfos.add(info);
		}
		return replyInfos;
	}
}
